package com.mv.controller;

import com.mv.pojo.Card;
import com.mv.pojo.Movie;
import com.mv.pojo.User;
import com.mv.service.CardService;
import com.mv.service.DealService;
import com.mv.service.MovieService;
import com.mv.vo.Form;
import com.mv.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@CrossOrigin
@RestController
@RequestMapping("/mv")
public class CardController {
    @Autowired
    private CardService cardService;
    /*查询会员卡个数*/
    @GetMapping("/card/getCardCount")
    public SysResult getCardCount(){
        Integer count=cardService.getCardCount();
        return SysResult.success(count);
    }
    /*查询所有的会员卡ID*/
    @GetMapping("/card/getCardId")
    public SysResult getCardId(){
        List<Card> cardList=cardService.getCardId();
        return SysResult.success(cardList);
    }
    /*根据会员卡ID查询会员卡余额*/
    @GetMapping("/card/getMoney")
    public SysResult getMoney(String id){
        Double money=cardService.getMoney(id);
        return SysResult.success(money);
    }
    /*查询会员的会员卡信息*/
    @GetMapping("/card/getCardList")
    public SysResult getCardList(Integer userId,String query){
        List<Card> cardList=cardService.getCardList(userId,query);
        return SysResult.success(cardList);
    }
    /*查找所有会员卡信息*/
    @GetMapping("/card/list")
    public SysResult findCard(String query){
        List<Card> cardList = cardService.FindCard(query);
        return SysResult.success(cardList);
    }
    /*封装会员和会员卡ID信息*/
    @GetMapping("/user/getUserAndCard")
    public SysResult getUserAndCard(){
        List<User> userList=cardService.getUserAndCard();
        return SysResult.success(userList);
    }
    /*更改会员卡状态*/
    @PutMapping("/card/updateStatus")
    public SysResult updateStatus(@RequestBody Card card){
        cardService.updateStatus(card);
        return SysResult.success();
    }
    /*会员卡充值*/
    @PutMapping("/card/updateMoney")
    public SysResult updateMoney(@RequestBody Form form){
        cardService.updateMoney(form);
        return SysResult.success();
    }
    /*会员卡消费*/
    @PutMapping("/card/updateCard")
    public SysResult updateCard(@RequestBody Form form){
        cardService.updateCard(form);
        return SysResult.success();
    }
    /*根据会员Id查询会员用户名*/
    @GetMapping("/card/getUserNameByUserId")
    public SysResult getUserNameByUserId(Integer userId){
        String userName=cardService.getUserNameByUserId(userId);
        return SysResult.success(userName);
    }
    /*1.会员卡信息增加*/
    @PostMapping("/card/add")
    public SysResult addCard(@RequestBody Form form){
        form.getCard().setId(String.valueOf((2136000000+Num())));
        cardService.addCard(form);
        return SysResult.success();
    }
    /*2.根据会员卡ID删除会员卡信息*/
    @DeleteMapping("/card/deleteCard")
    public SysResult deleteCard(Card card){
        Boolean flag = cardService.deleteCard(card);
        return SysResult.success(flag);
    }
    /*根据会员ID集合删除会员卡信息*/
    @PostMapping("/card/deleteBySelectList")
    public SysResult deleteBySelectList(@RequestBody List<User> selectList){
        cardService.deleteBySelectList(selectList);
        return SysResult.success();
    }


    public int Num() {
        String sources = "0123456789";
        Random rand = new Random();
        StringBuffer flag = new StringBuffer();
        for (int j = 0; j < 6; j++) {
            flag.append(sources.charAt(rand.nextInt(9)) + "");
        }
        int i = Integer.parseInt(flag.toString());
        List<String> idList = getId();
        for (String SqlId:idList) {
            int i1 = Integer.parseInt(SqlId);
            if (i == i1) Num();
        }
        return i;
    }
    public List<String> getId(){
        List<String> list = cardService.selectId();
        return list;
    }
}
